package com.idiot.operationbackend.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.idiot.operationbackend.entity.PopularizeQrCode;
import com.idiot.operationbackend.entity.Scene;
import com.idiot.operationbackend.mappers.PopularizeQrCodeMapper;
import com.idiot.operationbackend.mappers.SceneMapper;
import com.idiot.operationbackend.support.ScanScene;
import com.idiot.operationbackend.support.SceneVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class PopularQrCodeService {

    @Resource
    private PopularizeQrCodeMapper popularizeQrCodeMapper;
    @Resource
    private SceneMapper sceneMapper;

    @Transactional
    public String add(Scene scene){
        LocalDateTime now = LocalDateTime.now();
        scene.setCreateTime(now);
        sceneMapper.insert(scene);
        String sceneId = scene.getId();
        if(!CollectionUtils.isEmpty(scene.getPopularizeQrCodes())){
            for(PopularizeQrCode popularizeQrCode: scene.getPopularizeQrCodes()){
                //todo weixinAPI
                popularizeQrCode.setSceneId(sceneId);
                popularizeQrCode.setCreateTime(now);
            }
            popularizeQrCodeMapper.batchInsert(scene.getPopularizeQrCodes());
        }
        return sceneId;
    }

    public void editPopularizeQrCode(PopularizeQrCode popularizeQrCode){
        popularizeQrCodeMapper.updateById(popularizeQrCode);
    }

    public String addPopularizeQrCode(PopularizeQrCode popularizeQrCode){
        popularizeQrCode.setCreateTime(LocalDateTime.now());
        popularizeQrCodeMapper.insert(popularizeQrCode);
        return popularizeQrCode.getId();
    }

    public PageInfo<SceneVO> scenePage(ScanScene scanScene){
        PageHelper.startPage(scanScene.getPageNum(),scanScene.getPageSize());
        List<SceneVO> list = sceneMapper.list(scanScene);
        return new PageInfo<>(list);
    }


}
